package com.ruoyi.wisdom.event;

import com.ruoyi.wisdom.domain.IotSchoolClasses;
import com.ruoyi.wisdom.domain.swap.SwapSchoolClasses;
import com.ruoyi.wisdom.service.IIotSchoolClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SchoolClassesEvent extends IotEvent{

    @Autowired
    private IIotSchoolClassesService iotSchoolClassesService;

    /**
     * 处理
     * @param swapSchoolClasses
     */
    public void handle(SwapSchoolClasses swapSchoolClasses) {
        if (swapSchoolClasses.getOperate() == null) {
            save(swapSchoolClasses);
        } else {
            switch (swapSchoolClasses.getOperate()) {
                case "delete":
                case "remove":
                    delete(swapSchoolClasses);
                    break;
                default:
                    save(swapSchoolClasses);
            }
        }
    }

    /**
     * 添加|编辑
     * @param swapSchoolClasses
     */
    public void save(SwapSchoolClasses swapSchoolClasses)
    {
        IotSchoolClasses result = iotSchoolClassesService.selectIotSchoolClassesById(swapSchoolClasses.getId());
        // 实体装换
        IotSchoolClasses iotSchoolClasses = getModelMapper().map(swapSchoolClasses, IotSchoolClasses.class);
        if (result != null) {
            iotSchoolClasses.setId(swapSchoolClasses.getId());
            iotSchoolClassesService.updateIotSchoolClasses(iotSchoolClasses);
        } else {
            iotSchoolClassesService.insertIotSchoolClasses(iotSchoolClasses);
        }
    }

    /**
     * 删除
     * @param swapSchoolClasses
     */
    public void delete(SwapSchoolClasses swapSchoolClasses){
        iotSchoolClassesService.deleteIotSchoolClassesById(swapSchoolClasses.getId());
    }
}
